package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"happy-planet-cup/cache"
	"happy-planet-cup/models"
	"strconv"
	"time"
)

func CarAdd(c *gin.Context) {
	car_name := c.PostForm("car_name")
	car_status := c.PostForm("car_status")
	car_price, _ := strconv.Atoi(c.PostForm("car_price"))
	car_num, _ := strconv.Atoi(c.PostForm("car_num"))
	car_time := c.PostForm("car_time")
	data := models.Car{
		CarName:   car_name,
		CarStatus: car_status,
		CarPrice:  car_price,
		CarNum:    car_num,
		CarTime:   car_time,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func CarOrder(c *gin.Context) {
	order_no := c.PostForm("order_no")
	order_status := c.PostForm("order_status")
	order_name := c.PostForm("order_name")
	cid, _ := strconv.Atoi(c.PostForm("cid"))
	data := models.CarOrder{
		OrderNo:     order_no,
		OrderStatus: order_status,
		OrderName:   order_name,
		Cid:         cid,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func CarOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	var car []models.CarOrderList
	redis_key := "car_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("car_orders").
			Select("car_orders.id as OrderId",
				"cars.car_name as CarName",
				"cars.car_status as CarStatus",
				"cars.car_price as CarPrice",
				"cars.car_num as CarNum",
				"cars.car_time as CarTime",
				"car_orders.order_no as OrderNo",
				"car_orders.order_status as OrderStatus",
				"car_orders.order_name as OrderName",
			).
			Joins("LEFT JOIN cars on cars.id=car_orders.id").Offset(offset).Limit(size).
			Debug().Find(&car)
		jsonData, _ := json.Marshal(car)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &car)
	}
	var total int64
	models.DB.Model(&models.CarOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  car,
		"total": total,
	})
	return
}
func CarOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var car models.CarOrderList
	redis_key := "car_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("car_orders").
			Select("car_orders.id as OrderId",
				"cars.car_name as CarName",
				"cars.car_status as CarStatus",
				"cars.car_price as CarPrice",
				"cars.car_num as CarNum",
				"cars.car_time as CarTime",
				"car_orders.order_no as OrderNo",
				"car_orders.order_status as OrderStatus",
				"car_orders.order_name as OrderName",
			).
			Joins("LEFT JOIN cars on cars.id=car_orders.id").Where("car_orders.id=?", id).
			Debug().Find(&car)
		jsonData, _ := json.Marshal(car)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &car)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": car,
	})
	return
}
